home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 280 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Cleaning auto_ptr copy semantics.
  5. Date: 5 Feb 1996 00:09:26 GMT
  6. Organization: MAXTAL
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <31153AC5.1776@suphys.physics.su.oz.au>
  9. References: <01BAF15D.08B8EEE0@dino.int.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Mime-Version: 1.0
  12. Content-Type: text/plain; charset=us-ascii
  13. Content-Transfer-Encoding: 7bit
  14. X-Nntp-Posting-Host: slsyd8p04.ozemail.com.au
  15. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  16. Content-Length: 1171
  17. Originator: clamage@taumet
  18.  
  19. Eugene Lazutkin wrote:
  20. [auto_ptr]
  21.  
  22. > It's good if you are going to create a STL
  23. > container of auto_ptr's.  
  24.  
  25.     EGADS! NEVER DO THIS!!!!
  26.  
  27.     The following will CRASH FOR SURE:
  28.  
  29.     void f(vector<auto_ptr<X> >){}
  30.     vector<auto_ptr<X> > vx; vx.push_front(new X);
  31.     f(vx);
  32.     vx[0]; // CRASH
  33.  
  34. Copying the container transfers ownership to the NEW container.
  35. You must NOT use FIFO/block structure here: if the new
  36. container is destroyed before the old one, all the auto_ptrs
  37. in the old container dangle.
  38.  
  39. Even worse, if you have an input iterator onto the container
  40. which returns an rvalue, dereferencing it steals ownership
  41. from the container by creating a copy -- the destruction
  42. of which will delete the object, leaving the container's
  43. auto_ptr dangling. 
  44.  
  45. [Correct use of the idiom requires the newest copy of
  46. an auto_ptr outlive use of older ones. This is often
  47. achieved by assigning the newest value back to the object
  48. it was copied from.]
  49.  
  50. -- 
  51. John Max Skaller               voice: 61-2-566-2189
  52. 81 Glebe Point Rd              fax:   61-2-660-0850
  53. GLEBE NSW 2037                 web: http://www.maxtal.com.au/~skaller/
  54. AUSTRALIA                      email: skaller@maxtal.com.au
  55.  
  56. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  57.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  58.   summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  59. ]
  60.